package com.zxwl.common.util;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class StringUtil
{
	public static boolean isEmpty(String str)
	{
		if (null == str || str.trim().length() == 0 || "".equals(str.trim()))
		{
			return true;
		}

		return false;
	}

	public static boolean isValue(String str)
	{
		return !isEmpty(str);
	}

	/**
	 * 日期加操作，返回计算后的日期
	 */
	public static String addDate(String dateTime, int n)
	{
		SimpleDateFormat dd = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		try
		{
			date = dd.parse(dateTime);
		}
		catch (ParseException e)
		{
			e.printStackTrace();
		}
		Calendar calstart = Calendar.getInstance();
		calstart.setTime(date);
		calstart.add(Calendar.DAY_OF_MONTH, n);
		return dd.format(calstart.getTime());
	}

	/**
	 * 根据时间查询时，开始时间格式化
	 * 
	 * @param beginDate
	 * @return 开始时间
	 */
	public static Date dateTransferForBegin(Date beginDate)
	{
		Date startTime = null;
		String temp;
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (beginDate == null || beginDate.equals(""))
			{
				temp = "1900-01-01";
			}
			else
			{
				temp = sdf.format(beginDate);
			}
			String begin = temp + " 00:00:00";
			System.out.println("The begin is " + begin);
			startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(begin);

			return startTime;

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return startTime;
	}

	/**
	 * 根据时间查询时，结束时间格式化
	 * 
	 * @param endDate
	 * @return 结束时间
	 */
	public static Date dateTransferForEnd(Date endDate)
	{
		Date endTime = null;
		String temp;
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (endDate == null || endDate.equals(""))
			{
				temp = "2100-01-01";
			}
			else
			{
				temp = sdf.format(endDate);
			}
			String end = temp + " 23:59:59";
			System.out.println("The end is " + end);
			endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(end);
			return endTime;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return endTime;
	}

	/**
	 * 格式化时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date需格式化的时间
	 * @return 时间
	 */
	public static Date timeTransfer(Date date)
	{
		Date startTime = null;
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if (date == null || date.equals(""))
			{
				date = new Date();
			}
			String temp = sdf.format(date);
			System.out.println("The time is " + temp);
			startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(temp);
			return startTime;

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return startTime;
	}

	/**
	 * 功能描述：时间相减得到毫秒
	 * 
	 * @param beginDate
	 * @param date1
	 * @return date2
	 */
	public static long getDaySubTime(Date date1, Date date2)
	{
		long day = 0;
		day = date1.getTime() - date2.getTime();
		return day;
	}

	/**
	 * 功能描述：时间相减得到天数
	 * 
	 * @param beginDateStr
	 * @param endDateStr
	 * @return int
	 * @author yp
	 */
	public static int getDaySub(String beginDateStr, String endDateStr)
	{
		int day = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date beginDate;
		Date endDate;
		try
		{
			beginDate = format.parse(beginDateStr);
			endDate = format.parse(endDateStr);
			day = (int) ((endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000));
		}
		catch (ParseException e)
		{
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		return day;
	}
	
	/**
	 * 功能描述：时间相减得到天数
	 * 
	 * @param beginDate
	 * @param endDate
	 * @return int
	 * @author yp
	 */
	public static int getDaySub(Date beginDate, Date endDate)
	{
		int day = 0;
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		try
		{
			String beginDateStr = format.format(beginDate);
			String endDateStr = format.format(endDate);

			day = getDaySub(beginDateStr, endDateStr);

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return day;
	}

	/**
	 * 
	 * @param je
	 * @param def
	 * @return
	 * @author cbs
	 */
	public static String formatDouble(Double je, String def) 
	{
		if (je == 0.00) 
		{
			return def;
		}
		DecimalFormat fmt = new DecimalFormat("###,##0.00");
		return fmt.format(je);
	}
	
	/**
	 * 
	 * @param obj
	 * @return
	 * @author cbs
	 */
	public static String ToString(Object obj) {
		String reString = "";
		if (obj != null)
			reString = obj.toString();
		return reString;
	}

	/**
	 * 
	 * @param str
	 * @return
	 * @author cbs
	 */
	public static double strToFloat(String str) {
		double re = 0.0;
		try {
			re = Double.parseDouble(str);
		} catch (Exception e) {
			re = 0.0;
		}
		return re;
	}
	
	
	/**
	 * @param str
	 * @return
	 * @author cbs
	 */
	public static int strToInt(String str) {
		int re = 0;
		try {
			re = Integer.parseInt(str);
		} catch (Exception e) {
			re = 0;
		}
		return re;
	}
	
	/**
	 * 按照指定的格式 格式化日期时间
	 * @param type 日期时间格式
	 * @param date 要格式化的日期时间
	 * @return String
	 */
	public static String formatDate(String type, Date date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat(type);
		String newDate = "";
		try
		{
			newDate = sdf.format(date);
		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}
		return newDate;
	}
}
